import React from 'react';
import moment from 'moment';
import { Input } from 'antd';
import Pre from '@/components/Pre';

export const queryStringToAry = str => (str ? str.split(',') : []);

export const queryAryToString = ary => (ary ? ary.join(',') : undefined);

export const renderFormText = text => text || (text === 0 ? text : '--');

export const renderFormItem = (editContent, viewContent, isEdit = false) =>
    isEdit ? editContent : renderFormText(viewContent);

export const renderHiddenField = ({ form, field, initialValue }) =>
    form.getFieldDecorator(field, {
        initialValue
    })(<Input type="hidden" />);

export const renderOptionValue = (list = [], value, field = 'text') =>
    renderFormText(list.find(item => String(item.value) === String(value))?.[field]);

export const renderTextArea = value => <Pre>{renderFormText(value)}</Pre>;

export const renderSwitchValue = value => (value === 1 ? '是' : '否');

export const threePartParser = value => value.replace(/\$\s?|(,*)/g, '');

export const renderPartTimeDuty = list => {
    if (!list || !list.length) return 0;
    return list.filter(item => item.dutyType === 0).length;
    // if (!list || !list.length) return '';
    // return list
    //     .filter(item => item.dutyType === 0)
    //     .map(item => item.dutyAlias)
    //     .join(' / ');
};

export const momentToString = (date, format = 'YYYY-MM-DD HH:mm', showEmpty = false) => {
    if (showEmpty && !date) {
        return '';
    }
    return moment(date).format(format);
};

export const getLeafId = (str = '') => Number(str.split('-').pop());

export const getPath = (ary = []) => ary.join('-');

// eslint-disable-next-line no-mixed-operators
export const MAX_NUMBER = 99 * 10 ** 8;

export const OFFICE_FILE_TYPES = [
    'doc',
    'docx',
    'pdf',
    'jpg',
    'jpeg',
    'png',
    'gif',
    'xls',
    'xlsx',
    'ppt',
    'pptx',
    'rar',
    'zip',
    '7z'
];
